<?php

namespace app\agent\controller;

use app\common\controller\Adminbase;
use app\Constant\ApiConstant;
use app\Constant\OrderConstant;
use app\model\Order as OrderModel;
use app\service\AgentService;
use think\Db;
use util\Logger;

class Order extends Adminbase
{
    public function index()
    {
        $model = 'Order';
        $agent = AgentService::getAgentInfo(UID);

        if ($this->request->isAjax()) {
            $map      = $this->buildParamsMap();
            $page     = input('page') ? input('page') : 1;
            $pageSize = input('limit') ? input('limit') : 20;

            $query       = OrderModel::where($map)->where('agent_id', $agent['id']);
            $count_query = clone $query;

            $list  = $query
                ->field('*')
                ->withAttr('total', function ($value) {
                    return $value > 0 ? number_format($value / 100, 2) : 0;
                })
                ->withAttr('agent_commission', function ($value, $data) {
                    if ($data['agent_shop_agent'] != $data['agent_id']) {
                        $commission = $data['agent_commission'] - $data['agent_shop_commission'];
                    } else {
                        $commission = $value;
                    }
                    return $commission > 0 ? number_format($commission / 100, 2) : 0;
                })
                ->withAttr('commission', function ($value, $data) {
                    //直推或自购佣金 = 直推或自购佣金 - 商品补贴佣金
                    return $value > 0 ? number_format(($value - $data['subsidy_commission']) / 100, 2) : 0;
                })
                ->withAttr('one_commission', function ($value) {
                    return $value > 0 ? number_format($value / 100, 2) : 0;
                })
                ->withAttr('subsidy_commission', function ($value) {
                    return $value > 0 ? number_format($value / 100, 2) : 0;
                })
                ->withAttr('status', function ($value) {
                    return OrderConstant::ORDER_STATUS[$value];
                })
                ->withAttr('commission_status', function ($value) {
                    return OrderConstant::COMMISSION_STATUS[$value];
                })
                ->page($page, $pageSize)
                ->order('id', 'desc')
                ->select();
            $total = $count_query->count();

            foreach ($list as &$item) {
                //利润 = 合作商佣金 - 直推或自购佣金 - 商品补贴佣金 - 推客上级分销佣金
                $profit = $item['agent_commission'] - $item['commission'] - $item['one_commission'];
                $item['profit'] = number_format($profit, 2);
            }

            $result = ["code" => 0, "count" => $total, "data" => $list];
            return json($result);
        }

        $status  = OrderConstant::ORDER_STATUS;
        $tabList = [
            ['key' => '', 'value' => '全部'],
        ];
        foreach ($status as $key => $value) {
            $tabList[] = [
                'key'   => $key,
                'value' => $value,
            ];
        }

        //生成列表数据
        $builder = new \app\common\controller\ListBuilder();
        $builder
            ->setDataUrl()
            ->setAddBtn(false)
            ->setDelAllBtn(false)
            ->setDelBtn(false)
            ->setTabNav($tabList)
            ->setTabName('status')
            ->setLimit()
            ->addSearchItem('order_no', 'text', '订单号')
            ->addSearchItem('commission_status', 'select', '佣金状态', OrderConstant::COMMISSION_STATUS)
            ->setModel($model)
            ->addTableCheckBox()
            ->addTableColumn('id', 'ID', null, null, true)
            ->addTableColumn('order_id', '订单号', 180)
            ->addTableColumn('user_id', '推客ID')
            ->addTableColumn('title', '商品名称', 300)
            ->addTableColumn('commission_status', '佣金状态', 100)
            ->addTableColumn('status', '订单状态', 100)
            ->addTableColumn('total', '订单金额', 150)
            ->addTableColumn('agent_commission', '订单总佣金', 150)
            ->addTableColumn('commission', '推客佣金', 150)
            ->addTableColumn('one_commission', '推客上级分销佣金', 150)
            ->addTableColumn('profit', '利润', 150)
            ->addTableColumn('create_time', '创建时间', 180)
            ->returnForm();

        $this->assign('module', 'finance');
        return $this->fetch('admin@common/lists');
    }
}
